#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import ttk
import os

def get_text(inputtext):
    for i in '!@#$%^&*()_¯+-;:`~\'"<>=./?,\n\t\r':
        inputtext = inputtext.replace(i, ' ')
    return inputtext.split()
def get_exclude():
    with open(os.getcwd()+'\\'+'wordcount.txt','r',encoding='UTF-8') as f:
        excludeword = f.read().lower()
        exclud = get_text(excludeword)
        return exclud
excludlist = get_exclude()


#主窗体
top = tk.Tk()
top.title("叠加工具")
top.geometry("600x700")
# top.resizable(0, 0)

#添加标签页
top.columnconfigure(0,weight=1)
top.rowconfigure(0,weight=1)
textFrame = ttk.LabelFrame(top)
textFrame.grid(column=0, row=0, sticky=tk.NSEW)
textFrame.columnconfigure(0, weight=1)
textFrame.rowconfigure(0, weight=1)
textFrame.rowconfigure(1, weight=6)
textFrame.rowconfigure(2, weight=1)
textFrame.rowconfigure(3, weight=4)
textFrame.rowconfigure(4, weight=2)

textRange = ttk.Label(textFrame, text='Paste Here', font=('Arial', 11))
textRange.grid(column=0, row=0, sticky=tk.W)
textRange.rowconfigure(0, weight=1)
textRange.columnconfigure(0, weight=1)

def wdcount(inputtext):
    ls = get_text(inputtext)
    counts = {}
    for i in ls:
        if i not in excludlist:
                counts[i] = counts.get(i, 0) + 1
    iteams = list(counts.items())
    iteams.sort(key=lambda x: x[1], reverse=True)
    return iteams

def countfunc():
    clearTree()
    resultlist = wdcount(getContext())
    for result in resultlist:
        treevv.insert('', 'end',values=[result[1], result[0]])

def clearfunc():
    clearContext()
    clearTree()

def getContext():
    textContext=textEntered.get('0.0', tk.END).lower()
    return textContext

def clearContext():
    textEntered.delete('1.0', tk.END)

def clearTree():
    x = treevv.get_children()
    for item in x:
        treevv.delete(item)
textEntered = tk.Text(textFrame)
textEntered.grid(column=0, row=1,padx=(0,10),pady=(10,10),  sticky=tk.NSEW)
textEntered.rowconfigure(0, weight=1)
textEntered.columnconfigure(0, weight=1)

countRange = ttk.Label(textFrame, text='Word Count', font=('Arial', 11))
countRange.grid(column=0, row=2, sticky=tk.W)
countRange.rowconfigure(0, weight=1)
countRange.columnconfigure(0, weight=1)

resultFrame = ttk.LabelFrame(textFrame)
resultFrame.grid(row=3, column=0, sticky=tk.NSEW)
resultFrame.rowconfigure(0, weight=1)
resultFrame.columnconfigure(0, weight=29)
resultFrame.columnconfigure(1, weight=1)

resultscr = ttk.Scrollbar(resultFrame, orient=tk.VERTICAL)
resultscr.grid(row=0, column=1, padx=(0, 10), pady=(0, 0), sticky=tk.NS)


treevv = ttk.Treeview(resultFrame, columns=('c1','c2'), show="headings", yscrollcommand=resultscr.set)
treevv.column('c1', width=60, anchor=tk.W,stretch=True)
treevv.column('c2', width=200, anchor=tk.W,stretch=True)

treevv.heading('c1', anchor=tk.W, text='counts')
treevv.heading('c2', anchor=tk.W, text='word')

treevv.grid(row=0,column=0,pady=(5,0),sticky=tk.NSEW)
resultscr.config(command=treevv.yview)

buttomRange = ttk.LabelFrame(textFrame)
buttomRange.grid(column=0, row=4, sticky=tk.NSEW)
buttomRange.columnconfigure(0,weight=1)
buttomRange.columnconfigure(1,weight=3)
buttomRange.columnconfigure(2,weight=1)
buttomRange.rowconfigure(0,weight=1)

countButtom = ttk.Button(buttomRange, text='Count', command=countfunc)
countButtom.grid(column=0,row=0,sticky=tk.NSEW)

clearButtom = ttk.Button(buttomRange, text='Clear', command=clearfunc)
clearButtom.grid(column=2,row=0,sticky=tk.NSEW)


top.mainloop()